let utils = {
  /**
   * @msg: 时间戳转年月日
   * @param {String | Number} time 时间戳
   * @param {Boolean} type  是否是linux时间戳
   * @return: Object
   */
  getTime(time, type) {
    var date;
    if (type) {
      time = this.iosDate(time);
      date = new Date(time);
    } else {
      date = new Date(time * 1000);
    }
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();
    return {
      date,
      year,
      month,
      day,
      ymd: year + "-" + this.setToTime(month) + "-" + this.setToTime(day),
      ymdhms:
        year +
        "-" +
        this.setToTime(month) +
        "-" +
        this.setToTime(day) +
        " " +
        this.setToTime(hour) +
        ":" +
        this.setToTime(min) +
        ":" +
        this.setToTime(sec),
      ymdhm:
        year +
        "-" +
        this.setToTime(month) +
        "-" +
        this.setToTime(day) +
        " " +
        this.setToTime(hour) +
        ":" +
        this.setToTime(min),
      mdhms:
        this.setToTime(month) +
        "-" +
        this.setToTime(day) +
        " " +
        this.setToTime(hour) +
        ":" +
        this.setToTime(min) +
        ":" +
        this.setToTime(sec),
      mdhm:
        this.setToTime(month) +
        "-" +
        this.setToTime(day) +
        " " +
        this.setToTime(hour) +
        ":" +
        this.setToTime(min),
      hs: this.setToTime(hour) + ":" + this.setToTime(min),
      md: this.setToTime(month) + "/" + this.setToTime(day)
    };
  },

  //时间补零函数
  setToTime(num) {
    return num < 10 ? "0" + num : num;
  },

  // 深拷贝
  deepClone(obj) {
    let newObj = obj.push ? [] : {};
    for (var key in obj) {
      if (obj[key] instanceof Object) {
        newObj[key] = this.deepClone(obj[key]);
      } else {
        newObj[key] = obj[key];
      }
    }
    return newObj;
  },

  // 函数节流，处理高频触发事件性能问题
  /* 
      func：Function   触发的函数
      wait: Number     触发间隔
  */
  throttle(func, wait) {
    var ctx, args, timeoutID;
    var last = 0;

    return function throttled() {
      ctx = this;
      args = arguments;
      var delta = new Date() - last;
      if (!timeoutID) {
        if (delta >= wait) {
          call();
        } else {
          timeoutID = setTimeout(call, wait - delta);
        }
      }
    };

    function call() {
      timeoutID = 0;
      last = +new Date();
      func.apply(ctx, args);
      ctx = null;
      args = null;
    }
  },

  //毫秒转时分秒
  ms2time(diff, type) {
    var hour = 0;
    var min = 0;
    var sec = 0;
    if (type == "ms") {
      min = Math.floor(diff / 1000 / 60);
      sec = Math.floor((diff / 1000) % 60);
    } else {
      hour = Math.floor(diff / 1000 / 60 / 60);
      min = Math.floor((diff / 1000 / 60) % 60);
      sec = Math.floor((diff / 1000) % 60);
    }
    return {
      h: hour,
      m: min,
      s: sec,
      hour: this.setToTime(hour),
      min: this.setToTime(min),
      sec: this.setToTime(sec),
      hms: `${this.setToTime(hour)}:${this.setToTime(min)}:${this.setToTime(
        sec
      )}`,
      ms: `${this.setToTime(min)}:${this.setToTime(sec)}`
    };
  }
};

export default {
  install(vm) {
    vm.prototype.$utils = utils;
  }
};
